//
//  YZPigDocHealthViewModel.m
//  VET
//
//  Created by YZ on 2018/7/19.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDocHealthViewModel.h"
#import "YZPigDocHealthModel.h"

@implementation YZPigDocHealthViewModel

@synthesize dataSource = _dataSource;
@synthesize index = _index;

- (instancetype)init
{
    if(self = [super init]){
        [self config];
    }
    return self;
}

- (void)config
{
    _dataSource = @[[NSMutableArray array]];
}

- (void)headerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index = 0;
    YZWeakSelf(self);
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [self reqestHealthRecordListSuccess:^(NSDictionary *obj) {
        if (((NSArray *)((NSDictionary *)obj[@"data"])[@"list"]).count != 0) {
            weakself.totalPage = [((NSDictionary *)obj[@"data"])[@"lastPage"] integerValue] - 1;
        }else
        {
            weakself.totalPage = 0;
        }
        [arr removeAllObjects];
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDocHealthModel *model = [YZPigDocHealthModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(arr);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)footerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index++;
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [self reqestHealthRecordListSuccess:^(NSDictionary *obj) {
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDocHealthModel *model = [YZPigDocHealthModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(((NSDictionary *)obj[@"data"])[@"list"]);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)reqestHealthRecordListSuccess:(YZNetworkCompletionSuccessBlock)successBlock fail:(YZNetworkCompletionFailBlock)failBlock
{
    self.loading = YES;
    NSDictionary *params = @{@"index":[NSString stringWithFormat:@"%ld",_index],@"size":@"10",@"objectType":@"swine"};
    [YZNetworkManager getWithApi:[NSString stringWithFormat:@"/api/care/%@",self.pigId] params:params withHeaders:nil success:^(NSDictionary *obj) {
        self.loading = NO;
        if (successBlock) {
            successBlock(obj);
        }
    } fail:^(NSError *error) {
        self.loading = NO;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
@end
